//
// Created by Administrator on 2021/5/13.
//

#include <string>
#include <iostream>

using namespace std;

class Solution {
public:
    bool isValid(string s) {
        string st;
        for(auto x:s){
            if (x!='c') st.push_back(x);
            else{
                if (st.size() >= 2 and st.back() == 'b' and *(st.end()-2)=='a'){
                    st.pop_back();
                    st.pop_back();
                }
                else st.push_back(x);
            }
        }
        return st.empty();
    }
};

int main() {
    Solution sol;
    cout << sol.isValid("aabcbc") << endl;
    cout << sol.isValid("abcabcababcc") << endl;
    cout << sol.isValid("abccba") << endl;
    cout << sol.isValid("abacbcabcc") << endl;

    return 0;
}